import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';

import '../../../bean/good_entity.dart';
import '../../../store/user_state.dart';

abstract class BaseGoodComponent extends StatelessWidget {
  final GoodEntity good;
  bool showLabel = true;
  bool isKnight = UserState.isKnight;
  String price = '--';
  String orgPrice = '';
  bool showLine = false;
  int style = 0;

  BaseGoodComponent({Key? key, required this.good}) : super(key: key);

  @override
  StatelessElement createElement() {
    handlePrice();
    return super.createElement();
  }

  handlePrice() {
    //零售价
    var price = 0.0;
    var knightPrice = 0.0;
    if (good.showPriceType == 0) {
      price = good.price ?? 0.0;
    } else if (good.showPriceType == 1) {
      price = good.price ?? 0.00;
      knightPrice = good.showPrice ?? 0.00;
    } else if (good.showPriceType == 2) {
      //showPriceType==2会员价(showPrice为零售价，price为会员价)
      price = good.showPrice ?? 0.00;
      knightPrice = good.price ?? 0.00;
    }
    var privatePrice = getPrivatePrice(Decimal.parse(knightPrice.toString()).toString());

    if (good.promotionType! > 0 && good.promotionPrice! > 0) {
      this.price = "¥${Decimal.parse(good.promotionPrice.toString()).toString()}";
      //展示价格类型0(零售价与会员价相等) 没有会员价
      if (good.showPriceType! == 0) {
        showLabel = false;
        //判断活动价格和零售价格的大小
        if (good.promotionPrice < price) {
          //活动价格小于零售价 则显示零售价为划线价
          showLine = true;
          orgPrice = "¥${Decimal.parse(price.toString()).toString()}";
        } else {
          // 零售价大于活动价的时候 ，再判断市场价 与 活动价的大小
          if (good.promotionPrice < good.marketPrice) {
            // 活动价格小于市场价则 市场价为划线价
            showLine = true;
            orgPrice = "¥${Decimal.parse(good.marketPrice.toString()).toString()}";
          } else {
            orgPrice = '';
          }
        }
      } else {
        // 会员价与零售价不相等的情况下，判断会员价格与活动价的大小
        if (knightPrice < good.promotionPrice) {
          // 会员价小于活动价 则 需要判断是否是会员，
          if (isKnight) {
            // 是会员则直接显示会员价格
            showLine = false;
            orgPrice = "¥${Decimal.parse(knightPrice.toString()).toString()}";
          } else {
            // 非会员 则显示 隐私数据
            orgPrice = '¥$privatePrice';
          }
        } else {
          // 活动价格大于会员价 则 需要隐藏会员价格的标签
          showLabel = false;
          // 这个时候又需要 判断
          //判断活动价格和零售价格的大小
          if (good.promotionPrice < price) {
            //活动价格小于零售价 则显示零售价为划线价
            showLine = true;
            orgPrice = "¥${Decimal.parse(price.toString()).toString()}";
          } else {
            // 零售价大于活动价的时候 ，再判断市场价 与 活动价的大小
            if (good.promotionPrice < good.marketPrice) {
              // 活动价格小于市场价则 市场价为划线价
              showLine = true;
              orgPrice = "¥${Decimal.parse(good.marketPrice.toString()).toString()}";
            } else {
              orgPrice = '';
            }
          }
        }
      }
    } else {
      this.price = "¥${Decimal.parse(good.price.toString()).toString()}";
      //展示价格类型0(零售价与会员价相等) 没有会员价
      if (good.showPriceType == 0) {
        showLabel = false;
        // 判断市场价与 零售价的大小
        if (price < good.marketPrice) {
          // 零售价小于市场价 则 划线价显示市场价
          showLine = true;
          orgPrice = "¥${Decimal.parse(good.marketPrice.toString()).toString()}";
        } else {
          orgPrice = '';
        }
      } else {
        //  有会员价的时候 判断会员价和零售价
        if (knightPrice < price) {
          showLabel = true;
          if (isKnight) {
            orgPrice = "¥${Decimal.parse(knightPrice.toString()).toString()}";
          } else {
            orgPrice = '¥$privatePrice';
          }
        } else {
          if (price < good.marketPrice!) {
            // 零售价小于市场价 则 划线价显示市场价
            showLine = true;
            orgPrice = "¥${Decimal.parse(good.marketPrice.toString()).toString()}";
          } else {
            orgPrice = '';
          }
        }
      }
    }
  }

  String getPrivatePrice(String knightPrice) {
    var memberPrice = knightPrice;
    var price = StringBuffer();
    for (var i = 0; i < memberPrice.length; i++) {
      if (knightPrice.length > 5) {
        if (i >= 3) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else if (knightPrice.length > 3) {
        if (i >= 2) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      } else {
        if (i >= 1) {
          price.write('*');
        } else {
          price.write(memberPrice[i]);
        }
      }
    }
    return price.toString();
  }
}
